package com.zlp.fdfs.boot.controller;


import com.zlp.fdfs.boot.entity.BaseResponse;
import com.zlp.fdfs.boot.entity.DataResponse;
import com.zlp.fdfs.boot.entity.ImageDelete;
import com.zlp.fdfs.boot.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.Optional;

/**
 * @author Lei.Zhang
 * @Title ImageController
 * description: TODO
 * @Date 2021/1/20 16:56
 */
@RestController
@Api(tags = {"文件管理"})
@RequestMapping("/api/image")
public class ImageController {
    @Autowired
    private ImageService imageService;

    @PostMapping(value = "upload")
    @ApiOperation(value = "文件上传", notes = "文件上传")
    public DataResponse<String> uploadImage(@RequestPart("file") MultipartFile file) throws FileNotFoundException {
        Optional<MultipartFile> multipartFile = Optional.ofNullable(file);

        String imageUrl = imageService.uploadImage(file);
        return new DataResponse<>(imageUrl);
    }

    @PostMapping(value = "deleted")
    @ApiOperation(value = "文件删除", notes = "文件删除")
    public BaseResponse deletedImage(@RequestBody ImageDelete imageDelete) {
        Optional<String> imageDeleteUrl = Optional.ofNullable(imageDelete.getUrl());
        imageService.deletedImage(imageDelete.getUrl());
        return new BaseResponse();
    }

}
